<template>
  <view class="comBox">
    <sickTitleCom></sickTitleCom>
    <view class="body">
      <view class="name titleBold">{{ info.name }}</view>
      <view class="sickList">
        <view
          v-for="item in itemData"
          :key="item.id"
          :class="[item.active ? 'sickSpan cur' : 'sickSpan', info.rowHalf ? 'rowHalf' : '']"
          @click="choseList(item)"
        >
          {{ item.name }}
          <span v-if="item.active" class="curImg"><img :src="curIcon" /></span>
        </view>
      </view>
      <!-- 下一题上一题按钮 -->
      <nextProBtnCom @nextBtn="nextBtn" @proBtn="proBtn"></nextProBtnCom>
    </view>
  </view>
</template>

<script>
import nextProBtnCom from '@/components/nextProButton';
import sickTitleCom from './sickTitleCom';
import { ajaxSend } from './ajaxHealth.js';
import curIcon from '@/static/icon/cur.png';
import { mapGetters } from 'vuex';
export default {
  components: {
    // 上一题下一题组件
    nextProBtnCom,
    sickTitleCom
  },
  props: {
    info: {
      type: Object,
      default: {}
    }
  },
  data() {
    return {
      mainData: {},
      itemData: [],
      curIcon: curIcon,
      noChose: false
    };
  },
  computed: {
    ...mapGetters(['active', 'assessForm'])
  },
  mounted() {
    let data = JSON.parse(JSON.stringify(this.info.options));
    data.map((item) => {
      item.active = false;
      return item;
    });
    this.itemData = data;
  },
  methods: {
    async nextBtn() {
      // 必填校验
      let check = this.itemData.some((item) => item.active);
      if (this.info.require && !check) {
        uni.showToast({ title: `请选择${this.info.name}`, icon: 'none' });
        return false;
      }
      let subData = [];
      this.itemData.forEach((item) => {
        if (item.active) {
          subData.push(item.id);
        }
      });

      let submitData = this.assessForm;
      submitData[this.info.key] = subData;
      // if (this.info.key === 'vegetableFruit2' || this.info.key === 'exerciseMode') {
      // submitData[this.info.key].push(this.checkValue);
      // 数组求和，因为后端不支持数组
      let numbers = submitData[this.info.key];
      const reduceNumbers = numbers.reduce(function (sum, number) {
        // sum2 前两个数的和
        return sum + number;
      }, 0);
      submitData[this.info.key] = reduceNumbers;
      // }
      // 获取健康指标指导意见 判断是出健康提示还是直接下一步 start
      const healthData = await ajaxSend({ [this.info.key]: reduceNumbers });
      let curActive = this.active + 1;
      if (healthData && healthData.length > 0) {
        // 如果有异常，则提示
        this.$store.commit('assessQuestion/SET_AJAXRESMESSAGE', healthData); // 存提示信息到vux，数组格式
        this.$store.commit('assessQuestion/SET_AJAXRES', true); // 开启提示
      } else {
        this.$store.commit('assessQuestion/SET_ACTIVE', curActive);
        this.$store.commit('assessQuestion/SET_AJAXRES', false);
      }
      this.$store.commit('assessQuestion/SET_ASSESSFORM', submitData);
      // 获取健康指标指导意见 判断是出健康提示还是直接下一步 end
    },
    proBtn() {
      let curActive = this.active - 1;
      this.$store.commit('assessQuestion/SET_ACTIVE', curActive);
    },
    choseList(val) {
      const indexs = this.itemData.findIndex((item) => item.id === val.id);

      if (val.id === 8) {
        this.itemData.map((item) => {
          if (item.id !== 8) {
            item.active = false;
          }
          return item;
        });
      } else {
        const qdId = this.itemData.findIndex((item) => item.id === 8);
        this.itemData[qdId].active = false;
      }
      this.itemData[indexs].active = !this.itemData[indexs].active;
    }
  }
};
</script>
<style lang="scss">
.comBox {
  background-color: #fff;
  .title {
    background: rgba(216, 246, 214, 1);
    color: rgba(0, 0, 0, 0.55);
    font-size: 32rpx;
    padding: 14rpx;
    text-align: center;
  }
  .body {
    padding: 24rpx;
    .name {
      font-size: 32rpx;
      padding: 24rpx 0;
    }
    .sickList {
      .sickSpan {
        display: inline-block;
        padding: 10rpx 18rpx;
        margin: 12rpx 16rpx;
        border: 2rpx solid rgba(178, 178, 178, 1);
        border-radius: 16rpx;
        font-size: 28rpx;
        position: relative;
        .curImg {
          position: absolute;
          width: 40rpx;
          height: 40rpx;
          right: 0;
          bottom: 0;
          border: none;
          img {
            width: 40rpx;
            height: 40rpx;
          }
        }
      }
      .rowHalf {
        width: 38%;
        text-align: center;
      }
      .cur {
        border: 2rpx solid rgba(242, 85, 89, 1);
        color: rgba(242, 85, 89, 1);
      }
    }
  }
}
.clear {
  clear: both;
  overflow: hidden;
  zoom: 1;
  height: 0px;
}
.lineHeight86 {
  height: 86rpx;
  line-height: 86rpx;
}
.size28 {
  font-size: 28rpx;
}
.titleBold {
  font-weight: 500;
}
</style>
